<?php
// +----------------------------------------------------------------------------
// | PHP@妖孽 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2015 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------
/**
 *@todo 活动管理0
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2015-7-13
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;
class ActivityController extends AdminController {
    
	/**
	 * @todo 活动列表
	 * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
	 */
	public function index(){
		$activity_name = strip_tags(I('activity_name'));
		$category_list = D('activity')->getActivityCategory();
		$map = array();//初始化查询条件
		if($activity_name!=''){
			$map['activity_name']    =   array('like', '%'.(string)$activity_name.'%');
		}
		$activity_category_id = (int)I('activity_category_id');
		if($activity_category_id !=0){
			$map['activity_category_id']    =   $activity_category_id;
		}
// 		$marking_status = I('marking_status');
// 		if(is_numeric($marking_status)){
// 			$map['marking_status']    =  (int) $marking_status;
// 		}
		$map['status']=array('gt',-1);
		$list = $this->lists(D('Activity'),$map,'activity_id desc');
		int_to_string($list,array_merge(C('ACTIVITY_STATUS'),array('is_online'=>array(1=>'线上活动',0=>'线下活动'))));
		$this->getCategoryLists();
		$this->assign('_list', $list);
		$this->meta_title= '活动管理';
		$this->display();
	}
	

	/**
	 * @todo 上传活动图片
	 * @param int pic_id 图片id
	 * @param int activity_id 活动id
	 * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
	 */
	public function uploadActivityPicture(){
		if(IS_POST){
			$obj = D('Activity');
			$res =$obj->uploadActivityPicture();
			if(!$res){
				$this->error($obj->getError());
			}else{
				$this->success('上传成功');
			}
		}else{
			$this->error('请不要非法操作~');
		}
	}
	
	/**
     * 设置一条或者多条数据的状态
     * @author PHP@妖孽  <dino_ma@163.com>
     */
    public function setStatus($Model=CONTROLLER_NAME){
        $ids    =   I('request.ids');
        $status =   I('request.status');
        $model    =   D('activity');
        $pk = $model->getPk();
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }

        //缓存处理
        $activity_id = is_array($ids) ? implode(',',$ids) : (int)$ids;
        D('Activity')->cacheChange($activity_id,0,2);
        
        $map[$pk] = array('in',$ids);
        switch ($status){
            case -1 :
            	//活动删除
            	$model->activityLogInsert($ids,7);
                $this->delete($model, $map, array('success'=>'删除成功','error'=>'删除失败'));
                break;
            case 0  :
            	//活动禁用
            	$model->activityLogInsert($ids,8);
                $this->forbid($model, $map, array('success'=>'禁用成功','error'=>'禁用失败'));
                break;
            case 1  :
            	//活动启用
            	$model->activityLogInsert($ids,9);
                $this->resume($model, $map, array('success'=>'启用成功','error'=>'启用失败'));
                break;
            default :
                $this->error('参数错误');
                break;
        }
    }
	
	/**
	 * @todo 活动审核
	 * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
	 */
	public function activityVerify(){
		//接收id
		$activity_id = (int)I('activity_id');
		if($activity_id ==0){
			$this->error('老妹.~你逗谁呐?');
		}
		$obj = D('Activity');
		if(IS_POST){
			$res =$obj->updateActivityDetail();
			if(!$res){
				$this->error($obj->getError());
			}else{
				$this->success('操作成功', U('Activity/activityVerify',array('activity_id'=>$activity_id)));
			}
		}else{
			$info = $obj->getActivityDetail(array('activity.status'=>1,'activity.activity_id'=>$activity_id),'activity_id,activity_name,activity_category_id,start_time,end_time,show_start_time,show_end_time,create_time,create_user_id,marking_time,marking_status,marking_user,expect_member_num,expect_money,real_member_num,real_join_member_num,real_money');
			if(empty($info)){
				$this->error('活动不存在或已被禁用.~');
			}
			$pic_list = $obj->getActivityPictureList(array('activity_id'=>$activity_id,'picture.status'=>1),'activity_id,pic_id,path');
			
			$theme_list = $obj->getThemeModule($activity_id);
			
			$log_list = $obj->getActivityLog($activity_id);
		
			int_to_string($log_list,C('ACTIVITY_LOG_STATUS'));
			$this->meta_title= '活动审核管理';
			$this->assign('list',$info);
			$this->assign('pic_list',$pic_list);
			$this->assign('log_list',$log_list);
			$this->assign('theme_list',$theme_list);
			$this->display('activityVerify');
		}
	}
	
	
	/**
	 * @todo 活动状态审核
	 * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
	 */
	public function activityMarkingStatus($method=null){
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if ( empty($id) ) {
			$this->error('请选择要操作的数据!');
		}
		$map['activity_id'] =   array('in',$id);
		$model = 'activity_detail';
		$data=array('marking_time'=>NOW_TIME,'marking_user'=>UID);
		$obj = D('Activity');
		switch ( strtolower($method) ){
			case 'coil'://下线
				//活动下线
				$a = $obj->activityLogInsert($id,6);
				$data['marking_status']=-1;
				$this->editRow($model,$data, $map);
				break;
			case 'refuse'://不通过
				//活动不通过
				$obj->activityLogInsert($id,4);
				$data['marking_status']=1;
				$this->editRow($model,$data, $map);
				break;
			case 'pass'://通过
				//活动通过
				$obj->activityLogInsert($id,3);
				$data['marking_status']=2;
				$this->editRow($model, $data, $map);
				break;
			case 'go'://上线
				//活动上线
				$obj->activityLogInsert($id,5);
				$data['marking_status']=3;
				$this->editRow($model,$data, $map);
				break;
			default:
				$this->error('参数非法');
		}
	}
	
	
	/**
	 * @todo 删除活动回顾图片
	 * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
	 */
	public function delActivityPic(){
		$obj = D('Activity');
		$res =$obj->delActivityPic();
		if(!$res){
			$this->error($obj->getError());
		}else{
			$this->success('操作成功', U('Activity/index'));
		}
	}
	
	/**
	 * @todo 获取分类列表(内部调用)
	 * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
	 */
	private function getCategoryLists(){
		if(S('sys_category_list_activity')){
			$category_list = S('sys_category_list_activity');
		}else{
			$category_list = D('activity')->getActivityCategory();
			S('sys_category_list_activity',$category_list);
		}
		$this->assign('category_list',$category_list);
	}
	
	/**
	 * @todo 获取活动列表(内部调用)
	 * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
	 */
	private function getActivityList($map=array('status'=>1),$order='sort desc',$field="activity_id,activity_name"){
	    $activity_list = D('activity')->getActivityList($map,$order,$field);
	    $this->assign('activity_list',$activity_list);
	}
	
	/**
	 * @todo 活动编辑
	 * @param int activity_id 活动主键
	 * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
	 */
	public function edit(){
		$id = (int)I('activity_id');
		if(IS_POST){
			$this->update();
		}else{
			if($id==0){
				$this->error('请不要非法访问~');
			}
			$this->getCategoryLists();
			$info = D('Activity')->getActivityDetail(array('activity_id'=>$id));
			//获取主表和附表信息
			$this->assign('list',$info);
			$this->meta_title= '更新活动';
			$this->display('edit');
		}
	}
	
	/**
	 * @todo 发布活动
	 * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
	 */
	public function add(){
		if(IS_POST){
			$this->update();
		}else{
			$this->getCategoryLists();
			$theme_list = D('Activity')->getThemeList();
			$this->assign('theme_list',$theme_list);
			$this->meta_title= '发布活动';
			$this->assign('list',null);
			$this->display('edit');
		}
	}
	
	/**
	 *@todo 更新活动(仅供内部调用)
	 *@author PHP@妖孽 <mashengjie@hrbjjhh.com>
	 */
	private  function update(){
		$obj = D('Activity');
		$res =$obj->update();
		if(!$res){
			$this->error($obj->getError());
		}else{
 			S('common_get_activity_sms_content',null);
			$this->success('操作成功', U('Activity/index'));
		}
	}
	
	/**
	 * @todo   展示功能
     * @author  fang
	 */
	public function activityLook(){
	    
	    $html = D('Activity')->getModuleList();
	    if(empty($html)){
	        $html = D('Activity')->getError();
	    }else{
	        $this->show($html);
	    }
	}
	
	/**
	 * @todo   创建活动通过历史模板和模块的录入
	 * @param int activity_id 活动id
	 * @param int theme_id    模板id
	 * @author fang
	 */
	public function createHistoryTheme(){

	    $activity_id = (int)I('activity_id');
	    $theme_id = (int)I('theme_id');
	    
	    if(empty($activity_id) || empty($theme_id)){
	        $this->error('参数错误！');
	    }
	    
	    $result = D('Activity')->createHistoryTheme($activity_id,$theme_id);
	    $data['status'] = 1;
	    if($result){
	        $data['msg'] = '创建成功！';
	    }else{
	        $data['status'] = 0;
	        $data['msg'] = D('Activity')->getError();
	    }
	    $this->ajaxReturn($data);
	}
	
	/**
	 * @todo   根据活动ID获取下属模板及模块
	 * @param  int activity_id 活动id
	 * @author fang
	 */
	public function getThemeModule(){

	    $activity_id = (int)I('activity_id');
	    if(empty($activity_id)){
	        $this->error('参数错误！');
	    }
	    
	    $result = D('Activity')->getThemeModule($activity_id);
	}

	/**
	 * @todo 活动下的模版列表
	 * @param int activity_id 活动id
     * @author  fang
	 */
	public function activityTheme(){
	    
	    $this->meta_title= '模板管理';
	    //条件字段
	    $activity_id = (int)I('activity_id');
	    $status = I('status');
		$theme_name = strip_tags(I('theme_name'));
		//初始化查询条件
	    $map = array();
	    $list = null;
	    if($theme_name != ''){
	        $map['theme_name'] = array('like', '%'.$theme_name.'%');
	    }
	    if($activity_id > 0){
	        $map['activity_id'] = $activity_id;
	    }
	    if($status != 9 && $status != ''){
	        $map['status'] = (int)$status;
	    }
	    if($map){
	        $list = $this->lists(D('ActivityThemeView'),$map,'activity_id desc,type asc,start_time asc','*');
	    }
	    
	    $this->getActivityList();
	    $this->assign('_list',$list);
	    $this->display('activityTheme');
	}
	
	/**
	 * @todo   模版添加(activityThemeAdd)
	 * @param  array $_POST
     * @author  fang
	 */
	public function activityThemeAdd(){
	    
	    if(IS_POST){
	        $url = (I('url'))? I('url') : U('Activity/activityTheme') ;
	        $theme = D('Activity');
	        $result = $theme->themeUpdate();
	        if(false !== $result){	//更新数据
	            //添加记录行为
	            $this->success($result['message'], $url);
	        }else{
	            $error = $theme->getError();
	            $this->error(empty($error) ? '未知错误！' : $error);
	        }
	    }else{
		    $msg = "<jjhh>1</jjhh><jjhh>2</jjhh>\r\n标签中的 1，2，3为模块的标识\r\n模板替换<jjhh>*</jjhh>标签为模块内容"; 
		    $this->msg = $msg;
		    $this->meta_title= '创建模板';
	        $this->back_url = $_SERVER['HTTP_REFERER'];
		    $this->getActivityList();
		    $this->assign('info',null);
	        $this->display('activityThemeAdd');
	    }
	}
	
	/**
	 * @todo 获取活动的起止时间(不记录权限)
	 * @param int activity_id  活动id
	 * @author PHP@妖孽<mashengjie@hrbjjhh.com>
	 */
	public function getActivityTime(){
		if(IS_POST){
			$activity_id = (int)I('activity_id');
			if($activity_id ==0){
				$this->error('请不要非法操作');
			}
			$data = array('status'=>1);
			$list = M('activity')->field('show_start_time,show_end_time')->where(array('activity_id'=>$activity_id))->find();
			if(empty($list)){
				$data['status']=0;
				$this->ajaxReturn($data);
			}else{
			    $list['show_start_time'] = time_format($list['show_start_time']);
			    $list['show_end_time'] = time_format($list['show_end_time']);
				$data['data']=$list;
				$this->ajaxReturn($data);
			}
		}
	}
	
	
	/**
	 * @todo 模版编辑(activityThemeEdit)
	 * @param int activity_id 活动id
	 * @param int theme_id 模版id
     * @author  fang
	 */
	public function activityThemeEdit(){
	    
	    $activity_theme_id = (int)I('get.id');
	    
	    $where['activity_theme_id'] = $activity_theme_id;
	    $where['status'] = 1;
	    
	    $info = D('Activity')->getTheme($where);
	    if(!$info){
	        $this->error('模板不存在。');
	    }

	    $this->meta_title= '修改模板';
	    $this->getActivityList();
	    $this->back_url = $_SERVER['HTTP_REFERER'];
	    $this->assign('info',$info);
	    $this->display('activityThemeAdd');
	}
	
	/**
	 * @todo 模版状态修改
	 * @param int activity_id 活动id
	 * @param int theme_id 模版id
     * @author  fang
	 */
	public function activityThemeChangeStatus($method=null){
		if ( empty($_REQUEST['id']) ) {
			$this->error('请选择要操作的数据!');
		}
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if ( empty($id) ) {
		    $this->error('请选择要操作的数据!');
		}
		$where['activity_theme_id'] = array('in',$id);
		
		//缓存处理
		$where['theme_id'] = array('in',$id);
	    $activity_id = M('activity_theme_relation')->where($where)->getField('activity_id',true);
	    if($activity_id){
            D('Activity')->cacheChange($activity_id,0,2);
	    }
	    
		switch ( strtolower($method) ){
			case 'forbidtheme':
			    //禁用
                action_log('theme_disable', 'activity_theme', $id, UID);//记录行为
				$this->forbid('ActivityTheme', $where);
				break;
			case 'resumetheme':
			    //启用
			    action_log('theme_enable', 'activity_theme', $id, UID);//记录行为
				$this->resume('ActivityTheme', $where);
				break;
			case 'deletetheme':
			    //删除
			    $delete = D('Activity')->themeDelete($id);
			    if($delete){
			        action_log('theme_delete', 'activity_theme', $id, UID);//记录行为
			        $this->success('删除成功！', '', IS_AJAX);
			    }else{
			        $this->error('删除模板及相关模块失败！');
			    }
				break;
			default:
				$this->error($method.'参数非法');
		}
	}
	
	/**
	 * @todo 活动下的模版下的模块列表
	 * @param int activity_id 活动id
	 * @param int theme_id 模版id
     * @author  fang
	 */
	public function activityThemeModule(){
	    
		$this->meta_title= '模块管理';
	    //条件字段
	    $activity_id = (int)I('activity_id');
	    $theme_id = (int)I('activity_theme_id');
	    $status = I('status');
		$module_name = strip_tags(I('module_name'));
		//初始化查询条件
	    $map = array();
	    $list = null;
	    if($module_name != ''){
	        $map['module_name'] = array('like', '%'.$module_name.'%');
	    }
	    if($activity_id > 0){
	        $map['activity_theme_module.activity_id'] = $activity_id;
	    }
	    if($theme_id > 0){
	        $map['activity_theme_module.theme_id'] = $theme_id;
	    }
	    if($status != 9 && $status != ''){
	        $map['status'] = (int)$status;
	    }
	    $theme_list = D('Activity')->getThemeList();
	    
	    if($map){
	        $list = $this->lists(D('ActivityModuleView'),$map,'activity_id desc,theme_id desc,position asc,start_time asc','*');
	    }
	    
	    $this->getActivityList();
	    $this->assign('theme_list',$theme_list);
	    $this->assign('_list',$list);
	    $this->display('activityThemeModule');
	}
	
	
	/**
	 * @todo 活动下的模版下的模块添加
	 * @param int activity_id 活动id
	 * @param int theme_id 模版id
     * @author  fang
	 */
	public function activityThemeModuleAdd(){
	    
	    $activity_id = (int)I('activity_id');
	    $theme_id = (int)I('theme_id');
	    $position_id = (int)I('position');

	    if($activity_id == 0 || $theme_id == 0){
            $this->error('参数错误：缺少活动ID和模板ID！');
	    }
	    
	    $where_activity['status'] = 1;
	    $where_activity['activity_id'] = $activity_id;
	    $activity_name = D('Activity')->getActivityName($where_activity);
	    $where_theme['status'] = 1;
	    $where_theme['activity_theme_id'] = $theme_id;
	    $theme_name = D('Activity')->getThemeName($where_theme);
	    $activity_theme = $activity_name.' - '.$theme_name;
	    
	    //模块位置列表
	    $wehre_positon['activity_id'] = $activity_id;
	    $wehre_positon['theme_id'] = $theme_id;
	    $positon = D('Activity')->getModulePosition($wehre_positon);
	    //获取时间范围
	    $where_theme['status'] = 1;
	    $where_theme['activity_theme_id'] = $theme_id;
	    $time = D('Activity')->getThemeTime($where_theme);
	    
	    $this->meta_title= '模块添加';
	    $this->activity_theme = $activity_theme;
	    $this->back_url = $_SERVER['HTTP_REFERER'];
	    $this->assign('time',$time);
	    $this->assign('info', null);
	    $this->assign('activity_id',$activity_id);
	    $this->assign('position_id',$position_id);
	    $this->assign('theme_id',$theme_id);
	    $this->assign('position',$positon);
	    $this->display('activityThemeModuleEdit');
	}
	
	
	/**
	 * @todo 活动下的模版下的模块添加
	 * @param int activity_id 活动id
	 * @param int theme_id 模版id
	 * @param int module_id 模块id
     * @author  fang
	 */
	public function activityThemeModuleEdit(){

	    if(IS_POST){
	        $url = (I('url'))? I('url') : U('Activity/activityThemeModule') ;
	        $module = D('Activity');
	        $result = $module->oneModuleUpdate();
	        if(false !== $result){	//更新数据
	            //添加记录行为
	            $this->success('更新成功！', $url);//$_SERVER['HTTP_REFERER']
	        }else{
	            $error = $module->getError();
	            $this->error(empty($error) ? '未知错误！' : $error);
	        }
	    }
	    
	    $activity_module_id = (int)I('get.id');
	     
	    $where['activity_module_id'] = $activity_module_id;
	    $where['status'] = 1;
	    
	    $info = D('Activity')->getModule($where);
	    if(!$info){
	        $this->error('模块不存在。');
	    }
	    $activity_theme = $info['activity_name'].' - '.$info['theme_name'];
	    
	    //获取时间范围
	    $where_theme['status'] = 1;
	    $where_theme['activity_theme_id'] = $info['theme_id'];
	    $time = D('Activity')->getThemeTime($where_theme);
	    
	    $this->meta_title= '模块编辑';
	    $this->back_url = $_SERVER['HTTP_REFERER'];
	    $this->activity_theme = $activity_theme;
	    $this->assign('position_id', 0);
	    $this->assign('time',$time);
	    $this->assign('info',$info);
	    $this->display('activityThemeModuleEdit');
	}
	
	
	
	/**
	 * @todo 模块状态修改
	 * @param int activity_id 活动id
	 * @param int theme_id 模版id
	 * @param int module_id 模块id
     * @author  fang
	 */
	public function activityThemeModuleChangeStatus($method=null){
	if ( empty($_REQUEST['id']) ) {
			$this->error('请选择要操作的数据!');
		}
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',',$id) : $id;
		if ( empty($id) ) {
		    $this->error('请选择要操作的数据!');
		}
		$where['activity_module_id'] =   array('in',$id);
		
		//缓存处理
		$where['module_id'] = array('in',$id);
		$activity_id = M('activity_theme_module')->where($where)->getField('activity_id',true);
		if($activity_id){
            D('Activity')->cacheChange($activity_id,0,2);
		}
		
		switch ( strtolower($method) ){
			case 'forbidtheme':
			    //禁用
			    action_log('module_disable', 'activity_module', $id, UID);//记录行为
				$this->forbid('ActivityModule', $where);
				break;
			case 'resumetheme':
			    //启用
			    action_log('module_enable', 'activity_module', $id, UID);//记录行为
				$this->resume('ActivityModule', $where);
				break;
			case 'deletetheme':
			    //删除
			    action_log('module_delete', 'activity_module', $id, UID);//记录行为
				$this->delete('ActivityModule', $where);
				break;
			default:
				$this->error($method.'参数非法');
		}
	}
	
	
	
	/**
	 * @todo 添加分类
	 * @access public
	 * @param string $pid 父id
	 * @return mixed
	 */
	public function addCategory(){
		$activity_category = D('ActivityCategory');
		$parent_id = (int)I('parent_id');
		if(IS_POST){ //提交表单
			if((($parent_id != 0) && (false == $activity_category->info($parent_id,'activity_category_id')))){
				$this->error('没找到父id请重试！');
			}
			$update_array = array(
				'category_name'=>I('category_name'),
				'parent_id'=>$parent_id,
				'sort' => I('sort'),
				'remark' => I('remark'),
				'uid'=>UID
			);
			if(false != $activity_category->update($update_array)){
				$this->success('新增成功！', U('category'));
			} else {
				$error = $activity_category->getError();
				$this->error(empty($error) ? '未知错误！' : $error);
			}
		} else {
			$cate = array();
			if($parent_id){
				/* 获取上级分类信息 */
				$parent_activity = $activity_category->info($parent_id, 'activity_category_id,category_name,status');
				if(!($parent_activity && 1 == $parent_activity['status'])){
					$this->error('指定的上级分类不存在或被禁用！');
				}
				$this->assign('parent_activity', $parent_activity);
			}
			/* 获取分类信息 */
			$this->assign('sub_url','addCategory');
			$this->assign('info', null);
			$this->assign('meta_title','活动分类管理');
			$this->assign('meta_title_c','新增活动分类');
			$this->display();
		}
	}
	
	/**
	 * @todo 修改分类
	 * @access public
	 * @param string $pid 父id
	 * @return mixed
	 * @author simon <mahuan@hrbjjhh.com>
	 */
	public function editCategory(){
		$activity_category = D('ActivityCategory');
		$activity_category_id = (int)I('activity_category_id');
		$parent_id = (int)I('parent_id');
		if(IS_POST){ //提交表单
			if(false !== $activity_category->update()){
				$this->success('修改成功！', U('category'));
			} else {
				$error = $activity_category->getError();
				$this->error(empty($error) ? '未知错误！' : $error);
			}
		} else {
			if(($activity_category_id == 0)){
				$this->error('参数错误。');
			}	
			$activity = $activity_category->info($activity_category_id, 'activity_category_id,category_name,status,sort,remark,parent_id');
			if(empty($activity)){
				$this->error('您要编辑的活动不存在。');
			}	
			$cate = array();
			if($parent_id){
				/* 获取上级分类信息 */
				$parent_activity = $activity_category->info($parent_id, 'activity_category_id,category_name,status');
				if(!$parent_activity || 1 != $parent_activity['status']){
					$this->error('指定的上级分类不存在或被禁用！');
				}
				$this->assign('parent_activity', $parent_activity);
			}
			/* 获取分类信息 */
			$this->assign('sub_url','editCategory');
			$this->assign('activity', $activity);
			$this->assign('meta_title','活动分类管理');
			$this->assign('meta_title_c','编辑活动分类');
			$this->display();
		}
	}
	
	/**
	 * @todo 更改目录状态
	 * @access public
	 * @param string $activity_category_id 活动目录id
	 * @param string $status 状态码
	 * @return mixed
	 * @author simon <mahuan@hrbjjhh.com>
	 */
	public function setCategoryStatus(){
		$activity_category_id = (int)I('activity_category_id');
		$status = (int)I('status');
		if(($activity_category_id == 0)){
			$this->error('参数错误。');
		}
		$activity_category = D('ActivityCategory');
		$update_array = array(
			'activity_category_id'=>$activity_category_id,
			'status'=>$status
		);
		$res = $activity_category->validate(array(array('status',array(-1,0,1),'状态码不正确',1,'in')))->update($update_array);
		if($res != false){
			$this->success('操作分类成功！');
		}else{
			$error = $activity_category->getError();
            $this->error(empty($error) ? '未知错误！' : $error);
		}
	}
	
	/**
	 * @todo 显示分类树，仅支持内部调
	 * @access public
	 * @param string $tree 递归树
	 * @return view
	 * @author simon <mahuan@hrbjjhh.com>
	 */
	public function tree($tree = null){
		C('_SYS_GET_ACTIVITY_CATEGORY_TREE_') || $this->_empty();
		$this->assign('tree', $tree);
		$this->display('Activity:tree');
	}
	
	/**
	 * @todo 分类管理
	 * @access public
	 * @return view
	 * @author simon <mahuan@hrbjjhh.com>
	 */
	public function category(){
		$tree = D('ActivityCategory')->getTree(0,'activity_category_id,category_name,parent_id,status,create_time,uid,sort');
		$this->assign('tree', $tree);
		C('_SYS_GET_ACTIVITY_CATEGORY_TREE_', true); //标记系统获取活动分类树模板
		$this->assign('meta_title','活动分类管理');
		$this->display();
	}
	
	/**
	 * @todo 活动报名数据配置
	 * @param int activity_id 活动id
	 * @author PHP@妖孽<mashengjie@hrbjjhh.com>
	 */
	public function config($activity_id =0){
		$activity_id = (int)$activity_id;
		if($activity_id==0){
			$this->error('请不要非法访问');
		}
		$obj = D('Activity');
		$map = array('activity_id'=>$activity_id,'status'=>array('egt',0));
		if(!$obj->activityIs($map)){
			$this->error('当前活动已被禁用或删除.~');
		}
		//获取活动配置.list
		$field = 'activity_config_id,activity_id,activity_name,name,type,status,title,value';//开发期的时候先这样.~之后再处理.~
		$list = $this->lists(D('ActivityConfigView'),$map,'sort desc',$field);
		$this->assign('meta_title','活动报名字段配置管理');
		$this->assign('_list', $list);
		$this->display();
	}
	
	/**
	 * @todo 活动配置编辑
	 * @param int activity_id 活动id
	 * @author PHP@妖孽<mashengjie@hrbjjhh.com>
	 */
	public function configAdd(){
		$activity_id = (int)I('activity_id');
		if($activity_id ==0){
			$this->error('请不要非法访问~');
		}
		if(IS_POST){
			$this->configUpdate();
		}else{
			$obj = D('Activity');
			$activity_info = $obj->activityIs(array('status'=>1,'activity_id'=>$activity_id));
			if(empty($activity_info)){
				$this->error('当前活动已被禁用或删除.~');
			}
			$this->meta_title ='创建活动配置';
			$this->assign('activity_info',$activity_info);
			$this->assign('info',null);
			$this->display('configEdit');
		}
	}

	/**
	 * @todo 活动配置更新
	 * @param int activity_id 活动id
	 * @author PHP@妖孽<mashengjie@hrbjjhh.com>
	 */
	public function configEdit(){
		$config_id = (int)I('activity_config_id');
		if($config_id==0){
			$this->error('请不要非法访问~');
		}
		if(IS_POST){
			$this->configUpdate();
		}else{
			//查库
			$obj = M('ActivityConfig');
			$map = array('activity_config_id'=>$config_id,'status'=>array('egt',0));
			$info = $obj->where($map)->find();
			$activity_obj = D('Activity');
			$activity_info = $activity_obj->activityIs(array('status'=>1,'activity_id'=>$info['activity_id']));
			$this->assign('activity_info',$activity_info);
			$this->assign('info',$info);
			$this->meta_title ='更新活动配置';
			$this->display('configEdit');
		}
	}
	
	/**
	 * @todo 活动配置 创建/更新 实现
	 * @author PHP@妖孽<mashengjie@hrbjjhh.com>
	 */
	private function configUpdate(){
		$obj = D('ActivityConfig');
		$activity_id = (int)I('activity_id');
		if(!empty($obj->checkTableExist($activity_id))){
			$this->error('已生成报名表单不允许再进行修改~');
		}
		$res =$obj->update();
		if(!$res){
			$this->error($obj->getError());
		}else{
			$this->success('操作成功', U('config',array('activity_id'=>(int)I('activity_id'))));
		}	
	}

	/**
	 * @todo 设置活动配置状态
	 * @author PHP@妖孽<mashengjie@hrbjjhh.com>
	 */
	public function configSetStatus(){
		$obj = D('ActivityConfig');
		if(!empty($obj->checkTableExist((int)I('activity_id')))){
			$this->error('已生成报名表单不允许再进行修改~');
		}
		parent::setStatus(D('ActivityConfig'));
	}
	
	/**
	 * @todo  生成活动表单
	 * 注: 活动表单如果之前生成过,如果有数据的话,就不允许再次生成,如果没有数据的话,是重新创建数据表和表单
	 * @author PHP@妖孽<mashengjie@hrbjjhh.com>
	 */
	public function createForm(){
		if(IS_GET){
			$obj = D('Activity');
			if(!$obj->createForm()){
				$this->error($obj->getError());
			}else{
				$this->success('操作成功', U('config',array('activity_id'=>(int)I('activity_id'))));
			}
		}else{
			$this->error('想逗我玩?开玩笑一样.~');
		}
	}
	
	/**
	 * @todo 查看活动报名人数
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	public function activityJoinMember(){
		$activity_id = (int)I('activity_id');
    	if($activity_id ==0){
    		$this->error('请不要非法访问~');
    	}
    	$obj = D('Activity');
    	if(!$obj->activityIs(array('activity_id'=>$activity_id))){
    		$this->error('此活动不存在.~');
    	}
    	
    	$map = array('activity_id'=>$activity_id);
    	$serach_input = I('serach_input');
    	if($serach_input !=''){
    		if (preg_match("/^1[3-8][0-9]{9}$/",$serach_input)) {
    			$map['mobile_phone'] = array('eq',$serach_input);
    		}else{
    			$map['customer_name']=array('like','%'.$serach_input.'%');
    		}
    	}
    	
    	
    	//检查活动附属表是否生成.
    	$config_obj = D('ActivityConfig');
    	if($config_obj->checkTableExist($activity_id)){
    		//生成读取附属表
//     		$join_obj = M('ActivityJoin');
//     		$pre = C('DB_PREFIX');
//     		$total = $join_obj->where($map)->join(" left join {$pre}activity_attached_{$activity_id} on {$pre}activity_join.activity_join_id = {$pre}activity_attached_{$activity_id}.activity_join_id")->count();
    		
//     		$page = new \Think\Page($total, C('LIST_ROWS'));
//     		if($total>20){
//     			$page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
//     		}
//     		$p =$page->show();
//     		$list = $join_obj->where($map)->join(" left join {$pre}activity_attached_{$activity_id} on {$pre}activity_join.activity_join_id = {$pre}activity_attached_{$activity_id}.activity_join_id")->limit( $page->firstRow.','.$page->listRows)->select();
//     		$this->assign('_page', $p? $p: '');
//     		$this->assign('_total',$total);
//     		//获取活动配置.~
//     		$config_info = $config_obj->getActivityConfig($activity_id);
//     		$this->assign('config_info',$config_info);

    		
    		$join_obj =M('ActivityJoin');
    		$pre = C('DB_PREFIX');
    		$total = $join_obj->where($map)->join(" left join {$pre}activity_attached_{$activity_id} on {$pre}activity_join.activity_join_id = {$pre}activity_attached_{$activity_id}.activity_join_id")->
    		join("join {$pre}customer on {$pre}customer.customer_id = {$pre}activity_join.customer_id")->
    		join("join {$pre}customer_detail on {$pre}customer.customer_id = {$pre}customer_detail.customer_id")->
    		count();
    		$page = new \Think\Page($total, C('LIST_ROWS'));
    		if($total>20){
    			$page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
    		}
    		$p =$page->show();
    		$list = $join_obj->where($map)->join("left join {$pre}activity_attached_{$activity_id} on {$pre}activity_join.activity_join_id = {$pre}activity_attached_{$activity_id}.activity_join_id")->
    		join("join {$pre}customer on {$pre}customer.customer_id = {$pre}activity_join.customer_id")->
    		join("join {$pre}customer_detail on {$pre}customer.customer_id = {$pre}customer_detail.customer_id")->
    		limit( $page->firstRow.','.$page->listRows)->select();
    		$this->assign('_page', $p? $p: '');
    		$this->assign('_total',$total);
    		//获取活动配置.~
    		$config_info = $config_obj->getActivityConfig($activity_id);
    		$this->assign('config_info',$config_info);
    	}else{
    		$list = $this->lists(D('ActivityJoinView'),$map,'`activity_join_id` desc');
    		$this->assign('config_info',null);
    	}
    	int_to_string($list,array('ticket_level'=>array(0=>'普通票',1=>'VIP'),'sign_channel'=>array(0=>'主动报名',1=>'票点报名',2=>'代报名',3=>'活动现场报名',4=>'推广报名')));
		$this->meta_title='活动报名信息';
		$this->assign('_list',$list);
		$this->display('activityJoinMember');
	}
}

